/**
 * User: alesj
 * Date: 2005.6.2
 * Time: 11:34:33
 * 
 * (C) Genera Lynx d.o.o.
 */

package com.alesj.newsfeed.data;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.beans.BeanWrapperImpl;

public class EmailValidator implements Validator {

    private static final String EMAIL_PATTERN = "(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*";

    public boolean supports(Class clazz) {
        return true;
    }

    public void validate(Object obj, Errors errors) {
        String email = new BeanWrapperImpl(obj).getPropertyValue("email").toString();
        if (email == null) {
            throw new IllegalArgumentException("Email must be set!");
        }
        if (!email.matches(EMAIL_PATTERN)) {
            throw new IllegalArgumentException("Not an email pattern!");
        }
    }

}
